//
//  SKBoardGridView.m
//  Sudoku
//
//  Created by Richard Hodgkins on 18/09/2012.
//  Copyright (c) 2012 Richard Hodgkins. All rights reserved.
//

#import "SKBoardGridView.h"

#import "SKBoardConstants.h"

#define BOARD_INSET_SCALE 0.98

#define BOARD_BACKGROUND_ALPHA 0.6

#define CELL_COLOR UIColorFromRGB(0x555555)
#define BOX_COLOR_PENDING UIColorFromRGB(0x000000)
#define BOX_COLOR_COMPLETE UIColorFromRGB(0x00FF00)
#define BOX_COLOR_FAILED UIColorFromRGB(0xFF0000)
#define CELL_NONEDITABLE_COLOR UIColorFromGrayscale(0.2, 0)

@interface SKBoardGridView()

@property (nonatomic, strong) NSArray *fixedCellFrames;

@property (nonatomic, assign, readwrite) CGRect boardFrame;
// Board background that is only used when using a custom device
@property (nonatomic, strong) UIImage *boardBackground;

@end

@implementation SKBoardGridView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        self.backgroundColor = [UIColor clearColor];
        
        CGRect bounds = self.bounds;
        bounds.size.height *= BOARD_INSET_SCALE;
        bounds.origin.x = (self.frame.size.width - bounds.size.width) / 2.0;
        
        self.boardFrame = bounds;
        
        if ([SKUtils isCustomDevice]) {
            self.boardBackground = [UIImage imageNamed:@"board_background.png"];
        }
    }
    return self;
}

- (void) setFixedCellViews:(NSArray *) fixedCells
{
    NSMutableArray *fixedCellFrames = [NSMutableArray array];
    for (UIView *view in fixedCells) {
        [fixedCellFrames addObject:[NSValue valueWithCGRect:view.frame]];
    }
    self.fixedCellFrames = fixedCellFrames;
}

- (void)drawRect:(CGRect)rect
{
    [super drawRect:rect];
    
    CGRect bounds = self.boardFrame;
    CGFloat width = bounds.size.width, height = bounds.size.height;
    CGFloat originX = bounds.origin.x, originY = bounds.origin.y;
    CGFloat cellWidth = width * CELL_PER_COL_RECP, cellHeight = height * CELL_PER_ROW_RECP;
    CGFloat boxWidth = width * BOX_RECP_COLS, boxHeight = height * BOX_RECP_ROWS;
    
    // Draw board background first
    [self.boardBackground drawInRect:self.bounds blendMode:kCGBlendModeNormal alpha:BOARD_BACKGROUND_ALPHA];
    
    // Drawing code
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    
    // Draw cell outlines
    CGContextSaveGState(ctx);
    CGContextSetLineWidth(ctx, 1);
    CGContextSetStrokeColorWithColor(ctx, CELL_COLOR.CGColor);
    
    CGFloat x = originX, y = originY;
    int xEnd = x + width;
    for (uint i=1; i<CELLS_PER_ROW; i++) {
        y += cellHeight;
        CGContextMoveToPoint(ctx, (int) x, (int) y);
        CGContextAddLineToPoint(ctx, xEnd, (int) y);
    }
    
    x = originX, y = originY;
    int yEnd = y + height;
    for (uint i=1; i<CELLS_PER_COL; i++) {
        x += cellWidth;
        CGContextMoveToPoint(ctx,(int) x,( int) y);
        CGContextAddLineToPoint(ctx,(int) x, yEnd);
    }
    CGContextStrokePath(ctx);
    CGContextRestoreGState(ctx);
    
    
    // Highlight the editable cells
    CGContextSaveGState(ctx);
    CGContextSetLineWidth(ctx, 1);
    CGContextSetFillColorWithColor(ctx,  CELL_NONEDITABLE_COLOR.CGColor);
    for (NSValue *cell in self.fixedCellFrames) {
        CGContextAddRect(ctx, [cell CGRectValue]);
    }
    CGContextFillPath(ctx);
    CGContextRestoreGState(ctx);
    
    // Draw boxes
    UIColor *boxColor;
    switch (self.state) {
        case SKBoardStateCompleted:
            boxColor = BOX_COLOR_COMPLETE;
            break;
            
        case SKBoardStateFailed:
            boxColor = BOX_COLOR_FAILED;
            break;
            
        default:
            boxColor = BOX_COLOR_PENDING;
    }
    CGContextSaveGState(ctx);
    CGContextSetLineWidth(ctx, 2);
    CGContextSetStrokeColorWithColor(ctx, boxColor.CGColor);
    x = originX, y = originY;
    xEnd = x + width;
    for (uint i=0; i<=BOX_ROWS; i++) {
        CGContextMoveToPoint(ctx, x, y);
        CGContextAddLineToPoint(ctx, xEnd, y);
        y += boxHeight;
    }
    
    x = originX, y = originY;
    yEnd = y + height;
    for (uint i=0; i<=BOX_COLS; i++) {
        CGContextMoveToPoint(ctx, x, y);
        CGContextAddLineToPoint(ctx, x, yEnd);
        x += boxWidth;
    }
    CGContextStrokePath(ctx);
    CGContextRestoreGState(ctx);
}

-(void)setState:(SKBoardState)state
{
    if (_state != state) {
        _state = state;
        [self setNeedsDisplay];
    }
}
@end
